package com.ming_za.od_updater.core.window.page;

import com.ming_za.od_updater.core.window.LoadingState;
import com.ming_za.od_updater.core.window.component.LoadingIcon;
import com.ming_za.od_updater.core.updater.StateBridge;
import com.ming_za.od_updater.core.updater.UpdaterManager;
import com.ming_za.od_updater.core.window.PageManager;

import javax.swing.*;
import java.awt.*;

/**
 * 检查更新页面
 */
public class CheckUpdatePage implements IPage{
    @Override
    public void renderContent(JPanel panel) {

        panel.add(Box.createRigidArea(new Dimension(0, 10)));

        // 状态标签
        JLabel statusLabel = new JLabel(PageManager.checkUpdateText, JLabel.CENTER);
        statusLabel.setFont(statusLabel.getFont().deriveFont(16f));
        statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(statusLabel);

        panel.add(Box.createRigidArea(new Dimension(0, 10)));

        // 添加加载动画
        LoadingIcon loadingIcon = new LoadingIcon();
        loadingIcon.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(loadingIcon);

        panel.add(Box.createRigidArea(new Dimension(0, 10)));

        JButton closeButton = new JButton(PageManager.cancelText);
        closeButton.addActionListener(e -> {
            UpdaterManager.stopUpdate();
            StateBridge.setLoadingState(LoadingState.CONTINUE);
        });
        closeButton.setPreferredSize(new Dimension(60, 25));
        closeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(closeButton);

        panel.add(Box.createRigidArea(new Dimension(0, 10)));

        new Thread(UpdaterManager::startUpdate).start();
    }
}
